<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 会议控制模型
 * @author wwj
 * @copyright 2013-2-7
 */

class Conference extends MY_Controller
{
	function __construct()
	{
		parent::__construct();
		
		// 后台登录验证
		$this->check_login();
		
		// 验证权限
		$this->verify_purview(PURVIEW_CONFERENCE);
		
		$this->load->model('conference_m');
		
		// 加载文章的语言包
		$this->load->language('conference');
		
		$this->validate_data = array(
				array('field' => 'title', 'label' => 'lang:conference_title', 'rules' => 'trim|required|max_length[70]|xss_clean'),
				array('field' => 'begintime', 'label' => 'lang:conference_begin', 'rules' => 'trim|required|max_length[10]'),
				array('field' => 'finishtime', 'label' => 'lang:conference_finish', 'rules' => 'trim|required|max_length[10]'),
				array('field' => 'province', 'label' => 'lang:conference_address', 'rules' => 'trim|required|max_length[20]'),
				array('field' => 'city', 'label' => 'lang:conference_address', 'rules' => 'trim|required|max_length[20]'),
				array('field' => 'path', 'label' => 'lang:conference_attachment', 'rules' => 'trim|max_length[200]'),
				array('field' => 'content', 'label' => 'lang:conference_content', 'rules' => 'trim|required'),
				array('field' => 'keyword', 'label' => 'lang:com_keyword', 'rules' => 'trim|max_length[30]'),
				array('field' => 'classid', 'label' => 'lang:com_column_name', 'rules' => 'trim|required|is_natural_no_zero'),
				array('field' => 'releasename', 'label' => 'lang:com_releasename', 'rules' => 'trim|required|max_length[20]'),
				array('field' => 'remark', 'label' => 'lang:com_remark', 'rules' => 'trim|max_length[20]'),
				);
	}
	
	function index($page = '')
	{
		$this->manage($page);
	}
	
	/**
	 * 显示会议列表
	 * @param int $page 显示的页码
	 */
	function manage($page = '')
	{
		// 获取要显示的页数
		$page           = empty($page) ? 1 : abs(intval($page));
	
		// 组织分页信息
		$config['base_url']		= base_url('conference/manage');
		$config['total_rows']	= $this->conference_m->get_conference_count();
	
		$page_data = $this->get_page($config);
		$data['page'] 	= $page_data['page'];
		$order 			= 'istop desc,isrecommend desc,id desc';
	
		// 查询会议
		$conference      = $this->conference_m->show_conference($page_data['limit'], $order);
	
		// 会议信息列表数组
		$i = 1;
		foreach ($conference as $key => $row)
		{
			$conference[$key]['ordernum'] 	= ($page - 1) * $page_data['per_page'] + $i;
			$conference[$key]['foundingtime'] = date(DATE_FORMAT_CONFERENCE, $row['foundingtime']);
			$conference[$key]['begintime'] = date(DATE_FORMAT_CONFERENCE, $row['begintime']);
			$conference[$key]['finishtime'] = date(DATE_FORMAT_CONFERENCE, $row['finishtime']);
			$conference[$key]['cutofftime'] = date(DATE_FORMAT_CONFERENCE, $row['cutofftime']);
			$conference[$key]['createtime']	= date(DATE_FORMAT,$row['createtime']);
			$conference[$key]['updatetime']	= date(DATE_FORMAT,$row['updatetime']);
			$conference[$key]['isactive'] 	= $row['isactive'] == 0 ? $this->lang->line('com_sign_inactive') : $this->lang->line('com_sign_active');
			$conference[$key]['isverify'] 	= $row['isverify'] == 0 ? $this->lang->line('com_sign_not_verify') : $this->lang->line('com_sign_verify');
			$conference[$key]['isrecommend'] = $row['isrecommend'] == 1 ? $this->lang->line('com_sign_recommend') : '';
			$conference[$key]['istop'] 		= $row['istop'] == 1 ? $this->lang->line('com_sign_top') : '';
			$i++;
		}
	
		$data['header'] 		= $this->header;
		$data['title']			= $this->lang->line('conference_title_list');
		$data['page_label']		= $this->lang->line('conference_page_label');
		$data['nav_label'] 		= $this->lang->line('com_navigate_label');
		$data['conference'] 	= $conference;
		$this->load->view('conference_list',$data);
	}
	
	/**
	 * ajax删除会议，返回json结果
	 */
	function delete_action()
	{
		// 对表单数据做判断
		$validate = $this->validation(array(array('field' => 'id[]', 'label' => 'lang:conference_id', 'rules' => 'trim|required|is_natural|xss_clean')));
		if (TRUE !== $validate)
		{
			echo $this->array2json(TRUE, $validate);
			return ;
		}
		
		// 获取文章ID值，并放入数组
		$id = $this->input->post('id');
		if ($this->conference_m->delete_conference_by_id($id))
		{
			echo $this->array2json(FALSE, $this->lang->line('msg_operation_success'));
			return ;
		}
		else
		{
			echo $this->array2json(TRUE, $this->lang->line('msg_operation_failed'));
			return ;
		}
	}
	
	/**
	 * 显示添加会议页面
	 */
	function add()
	{
		$data['header'] 		= $this->header;
		$data['title']			= $this->lang->line('conference_title_add');
		$data['page_label']		= $this->lang->line('conference_page_label');
		$data['nav_label'] 		= $this->lang->line('com_navigate_label');
		$data['loginname'] 		= $this->loginname;
		$data['class'] 			= $this->get_column_as_array();
		$this->load->view('conference_add', $data);
	}
	
	/**
	 * 添加会议
	 */
	function add_action()
	{
		$validate = $this->validation($this->validate_data);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('conference/add'), $validate, 'goback');
		}
		
		$conference = $this->input->post();
		$content = array();
		$attachment = array();
		if ($conference && isset($conference['s_conference_add']))
		{
			// 判断添加的类别是否是子类
			if (!$this->has_sub_column($conference['classid']))
			{
				$this->_show_message(base_url('conference/add'), $this->lang->line('error_choice_sub_column'), 'goback');
			}
			
			// 组建附件数组
			if (!empty($conference['path']))
			{
				$attachment['title'] = $conference['title'];
				$attachment['path'] = $conference['path'];
				$attachment['type'] = 'conference';
			}
				
			$conference['updatetime'] = $conference['createtime'] = time();
			$conference['begintime'] = strtotime($conference['begintime']);	
			$conference['finishtime'] = strtotime($conference['finishtime']);
			$content['content'] = $conference['content'];
			$content['keyword'] = $conference['keyword'];
				
			// 清除数组中无用的数据，添加相关信息
			unset($conference['s_conference_add']);
			unset($conference['content']);
			unset($conference['keyword']);
			unset($conference['path']);

			// 添加
			$result = $this->conference_m->insert_conference($conference, $content, $attachment);
			if ($result)
			{
				$this->_show_message(base_url('conference'), $this->lang->line('msg_operation_success'));
			}
			else
			{
				$this->_show_message(base_url('conference/add'), $this->lang->line('msg_operation_failed'), 'goback');
			}
		}
		else
		{
			$this->_show_message(base_url('conference'), $this->lang->line('error_invalid_post'));
		}
	}
	
	/**
	 * 显示编辑会议页面
	 * @param int $id 会议id
	 */
	function edit($id)
	{
		$conference_id['conference.id'] = $id = intval($id);
		if (empty($conference_id))
		{
			$this->_show_message(base_url('conference'), $this->lang->line('conference_id_unable_empty'), 'goback');
		}
		
		// 查询信息
		$conference = $this->conference_m->get_conference($conference_id);		
		if (empty($conference))
		{
			$this->_show_message(base_url('conference'), $this->lang->line('conference_does_not_exist'), 'goback');
		}
		$conference = $conference[0];
		$conference['path'] = '';
		
		// 查询相对应的附件
		// @TODO 只选出一个，可以选出多个
		$this->load->model('attachment_m');
		$attachment = $this->attachment_m->get_attachment(array('bindid' => $id, 'type' => 'conference'));
		if (!empty($attachment))
		{
			$conference['path'] = $attachment[0]['path'];
		}
		$conference['begintime'] = date(DATE_FORMAT_CONFERENCE, $conference['begintime']);
		$conference['finishtime'] = date(DATE_FORMAT_CONFERENCE, $conference['finishtime']);
		$data = array (
			
				'header' => $this->header,
				'conference' => $conference,
				'class' => $this->get_column_as_array(),
				'loginname'	=> $this->loginname,
				'title' => $this->lang->line('conference_title_edit'),
				'page_label' => $this->lang->line('conference_page_label'),
				'nav_label' => $this->lang->line('com_navigate_label'),
				'redirect' => $this->get_referer_url(),
		);
		$this->load->view('conference_edit', $data);
	}
	
	/**
	 * 编辑会议
	 */
	function edit_action()
	{
		$this->load->model('attachment_m');
		$redirect = empty($_GET['redirect']) ? base_url('conference') : $_GET['redirect'];
		$validate = $this->validation($this->validate_data);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('conference/add'), $validate, 'goback');
		}
		
		$conference = $this->input->post();
		$content = array();
		$attachment = array();
		if ($conference && isset($conference['s_conference_edit']))
		{
			// 判断添加的类别是否是子类
			if (!$this->has_sub_column($conference['classid']))
			{
				$this->_show_message(base_url('conference/add'), $this->lang->line('error_choice_sub_column'), 'goback');
			}
			
			// 会议id不能为空
			$id = $conference['conference_id'];
			if (empty($id))
			{
				$this->_show_message(base_url('conference'), $this->lang->line('conference_id_unable_empty'), 'goback');
			}
			
			// 更新会议附件，不做旧附件的删除
			$attachment_count = $this->attachment_m->count_all_results(array('bindid' => $id, 'type' => 'conference'));
			if ($attachment_count >= 1)
			{
				// 编辑后，附件不为空，直接更新，否则删除原来的附件及表记录
				if (!empty($conference['path']))
				{
					$attachment['title'] = $conference['title'];
					$attachment['path'] = $conference['path'];
				}
				else
				{
					$attachment['bindid'] = 0;
				}
			}
			else
			{
				// 原来没有附件，编辑之后添加附件
				if (!empty($conference['path']))
				{
					$attachment['bindid'] = $id;
					$attachment['title'] = $conference['title'];
					$attachment['path'] = $conference['path'];
					$attachment['type'] = 'conference';
					$this->attachment_m->insert_attachment($attachment);
					$attachment = array();
				}
			}
			
			$conference['updatetime'] = time();
			$conference['begintime'] = strtotime($conference['begintime']);
			$conference['finishtime'] = strtotime($conference['finishtime']);
			$content['content'] = $conference['content'];
			$content['keyword'] = $conference['keyword'];
			
			// 清除数组中无用的数据，添加相关信息
			unset($conference['conference_id']);
			unset($conference['origin_classid']);
			unset($conference['s_conference_edit']);
			unset($conference['content']);
			unset($conference['keyword']);
			unset($conference['path']);
			
			// 更新
			$result = $this->conference_m->update_conference(array('id' => $id), $conference);
			$this->conference_m->update_conference_content(array('conferenceid' => $id), $content);
			if (!empty($attachment))
			{
				$result = $this->attachment_m->update_attachment(array('bindid' => $id, 'type' => 'conference'), $attachment);
			}
			
			if ($result)
			{
				$this->_show_message($redirect, $this->lang->line('msg_operation_success'));
			}
			else
			{
				$this->_show_message(base_url('conference'), $this->lang->line('msg_operation_failed'), 'goback');
			}
		}
		else
		{
			$this->_show_message(base_url('conference'), $this->lang->line('error_invalid_post'));
		}
	}
	
	/**
	 * 显示会议状态设置页面
	 * @param string $id 会议id
	 */
	function conference_state($id)
	{
		$id = (int)$id;
		$conference = $this->conference_m->get_conference_state(array('id' => $id), '1');
		if (empty($conference))
		{
			$this->_show_message(base_url('conference'), $this->lang->line('conference_does_not_exist'), 'goback');
		}
		$conference = $conference[0];
	
		$data = array (
					
				'header' => $this->header,
				'state' => $conference,
				'title' => $this->lang->line('conference_title_state_config'),
				'model' => 'conference'
		);
		$this->load->view('state_config', $data);
	}
	
	/* 
	 * @see MY_Controller::config_state()
	 */
	function config_state()
	{
		echo parent::config_state('conference_m', 'conference');
		return ;
	}
}

/* End of file conference.php */
/* Location: ./application/controllers/conference.php */